import React from 'react';
import { connect } from 'dva';
import { createForm } from 'rc-form';
import styles from './index.less';
import { Toast, Card, Button, Modal, WingBlank, WhiteSpace } from 'antd-mobile';
import { routerRedux } from 'dva/router';
import Block from 'fs-flex'
import Header from '../../../../components/Header/index'
import SysBusiTypeService from '../../../../services/sysBusiTypeService'
import BusiTypeService from "../../../../services/busiTypeService";

const alert = Modal.alert
class SettingPage extends React.Component {
  state = {
    show: false,
    data: []
  }

  async componentDidMount() {
    const { data, msg } = await SysBusiTypeService.allBusiType()
    this.setState({ data: data })
  }

  async delete(delData) {
    const { code, msg } = await SysBusiTypeService.deleteBusiType(delData)
    if (code === "0000") {
      Toast.info("删除成功！", 3)
      const { data, msg } = await SysBusiTypeService.allBusiType()
      this.setState({ data: data })
    } else {
      Toast.info(msg, 3)
    }
  }

  create() {
    console.log('click create!')
    this.props.history.push({ pathname: '/manage/addSysBusiType', state: { name: '新增业务类型' } })
  }

  async edit(datap) {
    const { data, msg } = await SysBusiTypeService.getBusiTypeData(datap)
    this.props.history.push({ pathname: '/manage/updateSysBusiType', state: { data: data } })
  }

  binding(data){
    this.props.history.push({pathname:'/manage/busiType/binding',state:{data:data} })
}

  render() {
    return (
      <Block vf>
        <Block>
          <Header needBack="ture" title='业务类型管理' history={this.props.history} />
        </Block>
        <WingBlank size="lg">
          <WhiteSpace size="lg" />
          {this.state.data && this.state.data.length > 0 ? this.state.data.map((data, idx) => (
            <Card key={idx}>
              <Card.Body>
                <Block vf>
                  <Block>{data.busitype}</Block>
                  <Block mt={10} wf>
                    <Block>
                      <Button type="primary" size="small" onClick={() => {
                        this.edit(data)
                      }} inline>编辑</Button>
                    </Block>
                    <Block ml={10}>
                      <Button type="primary" size="small" onClick={() => {
                        alert('删除确认', '确认删除该记录么？', [
                          { text: '取消', onPress: () => console.log('cancel') },
                          { text: '确认', onPress: () => this.delete(data) },
                        ])

                      }} inline>删除</Button>
                    </Block>
                    <Block ml={10}>
                        <Button type="primary" size="small" onClick={this.binding.bind(this,data)}inline>绑定说明文档</Button>
                    </Block>
                  </Block>
                </Block>
              </Card.Body>
            </Card>
          )) : null}
          <Block wf mt={15}>
            <Block f='3'></Block>
            <Block f='4'><Button a='c' f='c' size='small' type="primary" onClick={() => {
              this.create()
            }}
            >新建业务类型</Button></Block>
            <Block f='3'></Block>
          </Block>
        </WingBlank>
      </Block>
    );
  }

}

SettingPage
  .propTypes = {
  };

export default createForm()(connect()(SettingPage));
